package com.plumdong.news.service;

import com.plumdong.news.proto.NewServiceGrpc;
import com.plumdong.news.proto.NewsProto;
import io.grpc.stub.StreamObserver;

public class NewsService extends NewServiceGrpc.NewServiceImplBase {
    /**
     * @param request
     * @param responseObserver
     */
    @Override
    public void list(NewsProto.NewsRequest request, StreamObserver<NewsProto.NewsResponse> responseObserver) {
        String date = request.getDate();
        NewsProto.NewsResponse list = null;
        try {
            NewsProto.NewsResponse.Builder listBuilder = NewsProto.NewsResponse.newBuilder();
            for (int i = 0; i < 100; i++) {
                NewsProto.News news = NewsProto.News.newBuilder().setId(i)
                        .setContent(date + "当前新闻内容" + i)
                        .setTitle("新闻标题" + i)
                        .setCreateTime(System.currentTimeMillis())
                        .build();
                listBuilder.addNews(news);
            }
            list = listBuilder.build();

        }catch (Exception e){
            responseObserver.onError(e);
        }finally {
            responseObserver.onNext(list);
        }
        responseObserver.onCompleted();
    }
}
